#include "FlyWidget.h"

#include <QDateTime>
#include <QDebug>
#include <QPainter>
#include <QRandomGenerator>
#include <QScreen>

static QColor generateRandomColor() {
    // 生成随机RGB值，确保亮度适中，文本清晰可见
    int color[3];
    for (int i = 0; i < 3; i++) {
        color[i] = QRandomGenerator::global()->bounded(50, 256);
    }

    return QColor(color[0], color[1], color[2]);
}

static QPoint genRandomStartingPoint(int marginTopScreen) {
    int x, y;
    x = qApp->primaryScreen()->size().width();
    y = QRandomGenerator::global()->bounded(marginTopScreen);

    return QPoint(x, y);
}

FlyWidget::FlyWidget(QString str, QWidget* parent) : QWidget(parent) {
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint |
                   Qt::Tool);
    setAttribute(Qt::WA_TranslucentBackground);
    setAttribute(Qt::WA_DeleteOnClose);
    // setAttribute(Qt::WA_StyleSheet);
    // setStyleSheet("border-radius:10px;");
    m_str = str;

    QFont ft = font();
    ft.setPointSize(12);  // 设置字体大小
    ft.setBold(true);     // 加粗
    setFont(ft);

    m_bg = generateRandomColor();

    m_anm = new QPropertyAnimation(this, "geometry");
    connect(m_anm, &QPropertyAnimation::finished, this, [this]() { close(); });
    m_anm->setDuration(20000);
    m_start = genRandomStartingPoint(30);
    move(m_start);
}

void FlyWidget::paintEvent(QPaintEvent* e) {
    QPainter pt(this);
    pt.setRenderHint(QPainter::Antialiasing);
    pt.save();
    pt.setBrush(m_bg);
    pt.setPen(Qt::transparent);
    pt.drawRoundedRect(rect(), 20, 20);
    pt.restore();
    // 设置文本属性
    // 计算文本绘制位置（居中）
    QRect textRect = rect();
    pt.drawText(textRect, Qt::AlignCenter, m_str);
    QWidget::paintEvent(e);
}

QSize FlyWidget::sizeHint() const {
    QSize size;
    if (m_str.isEmpty()) {
        return QSize(m_margin * 2, m_margin * 2);  // 空文本时的默认大小
    }

    // 获取当前字体的度量信息
    QFontMetrics metrics(font());
    // 计算文本所需的宽度和高度
    int textWidth = metrics.horizontalAdvance(m_str);
    int textHeight = metrics.height();
    size = QSize(textWidth + m_margin * 2, textHeight + m_margin * 2);
    return size;
}

void FlyWidget::showEvent(QShowEvent* e) {
    m_anm->setStartValue(QRect(m_start, QSize(width(), height())));
    m_anm->setEndValue(
        QRect((-1) * width() - m_margin * 2, 50, width(), height()));
    m_anm->start();
    QWidget::showEvent(e);
}

#if (QT_VERSION <= QT_VERSION_CHECK(6, 0, 0))
void FlyWidget::enterEvent(QEvent*) { m_anm->pause(); }
#else
void FlyWidget::enterEvent(QEnterEvent*) { m_anm->pause(); }
#endif

void FlyWidget::leaveEvent(QEvent*) { m_anm->resume(); }

void FlyWidget::mousePressEvent(QMouseEvent* e) {
    auto button = e->button();
#if (QT_VERSION <= QT_VERSION_CHECK(6, 0, 0))
    QPoint pos = e->globalPos();
#else
    QPoint pos = e->globalPosition().toPoint();
#endif
    if (button == Qt::LeftButton) {
        emit clicked(m_str, button, pos);
    } else if (button == Qt::RightButton) {
        emit clicked(m_str, button, pos);
    }
}
